package com.cf.moudle.newsfeed.mvp.model;

import com.cf.base.http.HttpManager;
import com.cf.base.mvp.mvp.BaseModel;
import com.cf.base.mvp.rx.LifecycleTransformer;
import com.cf.moudle.newsfeed.mvp.api.UserApi;
import com.cf.moudle.newsfeed.mvp.bean.BaseInfo;
import com.cf.moudle.newsfeed.mvp.bean.UserInfo;
import com.cf.moudle.newsfeed.mvp.contracts.MainContract;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;


/**
 * @author yangji
 */
public class MainModel extends BaseModel<MainContract.Presenter> implements MainContract.Model {
    @Override
    public void login(String name, String pwd) {
        HttpManager.getInstance().create(UserApi.class)
                .login(name, pwd)
                .compose(bindEvent())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<BaseInfo<UserInfo>>() {
                    @Override
                    public void onNext(BaseInfo<UserInfo> userInfoBaseInfo) {
                        getPresenter().responseLogin(userInfoBaseInfo.value);
                    }

                    @Override
                    public void onError(Throwable e) {
                        getPresenter().responseLogin(null);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
